<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Technorati Service Driver Library
 * http://technorati.com/developers/api/
 *
 * $Id$
 *
 * @package		Service Module
 * @author		Alex Sancho
 * @copyright	(c) 2008 Alex Sancho
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its
 *	  contributors may be used to endorse or promote products derived
 *	  from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.	IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

class Service_Technorati_Driver extends Service_Driver 
{
	protected $api_url = "api.technorati.com";
	protected $api_key = null;

	/**
	 *	__construct
	 * 
	 * @param array $config
	 * @return void
	 * @access public
	 * 
	 */
	public function __construct($config = array()) 
	{
		$config += Kohana::config('service.technorati');

		foreach($config as $key => $val)
			$this->$key = $val;
	}

	/**
	 * cosmos
	 *
	 * @param string $url
	 * @param string $type
	 * @param integer $limit
	 * @param integer $start
	 * @param string $current
	 * @param string $format
	 * @param integer $claim
	 * @param integer $highlight
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function cosmos($url, $type = 'link', $limit = '20', $start = '1', $current = 'yes', $format = 'xml', $claim = '0', $highlight = '1')
	{
		$params = array
		(
			'key'		=> $this->api_key,
			'type'		=> $type,
			'limit'		=> $limit,
			'start'		=> $start,
			'current'	=> $current,
			'format'	=> $format,
			'claim'		=> $claim,
			'highlight' => $highlight
		);
		
		return $this->api_call('/cosmos/?'.http_build_query($params));
	}

	/**
	 * search
	 *
	 * @param mixed $query
	 * @param string $format
	 * @param string $language
	 * @param string $authority
	 * @param integer $start
	 * @param integer $limit
	 * @param integer $claim
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function search($query, $format = 'xml', $language = 'en', $authority = 'n', $start = '1', $limit = '20', $claim = '0')
	{			
		$params = array
		(
			'key'		=> $this->api_key,
			'query'		=> self::implode($query, "+"),
			'format'	=> $format,
			'language'	=> $language,
			'authority' => $authority,
			'start'		=> $start,
			'limit'		=> $limit,
			'claim'		=> $claim
		);

		return $this->api_call('/search/?'.http_build_query($params));
	}

	/**
	 * tag
	 *
	 * @param string $tag
	 * @param string $format
	 * @param integer $limit
	 * @param integer $start
	 * @param integer $excerptsize
	 * @param integer $topexcerptsize
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function tag($tag, $format = 'xml', $limit = '20', $start = '1', $excerptsize = '100', $topexcerptsize = '150')
	{
		$params = array
		(
			'key'				=> $this->api_key,
			'tag'				=> $tag,
			'format'			=> $format,
			'limit'				=> $limit,
			'start'				=> $start,
			'excerptsize'		=> $excerptsize,
			'topexcerptsize'	=> $topexcerptsize
		);

		return $this->api_call('/tag/?'.http_build_query($params));
	}
	
	/**
	 * daily_counts
	 *
	 * @param string $keywords
	 * @param integer $days
	 * @param string $format
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function daily_counts($keyword, $days = '180', $format = 'xml')
	{
		$params = array
		(
			'key'		=> $this->api_key,
			'q'			=> $keyword,
			'days'		=> $days,
			'format'	=> $format
		);

		return $this->api_call('/dailycounts/?'.http_build_query($params));
	}
	
	/**
	 * top_tags
	 *
	 * @param string $format
	 * @param integer $limit
	 * @param integer $start
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function top_tags($format = 'xml', $limit = '20', $start = '1')
	{
		$params = array
		(
			'key'		=> $this->api_key,
			'format'	=> $format,
			'limit'		=> $limit,
			'start'		=> $start
		);

		return $this->api_call('/toptags/?'.http_build_query($params));
	}

	/**
	 * blog_info
	 *
	 * @param string $url
	 * @param string $format
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function blog_info($url, $format = 'xml')
	{
		$params = array
		(
			'key'		=> $this->api_key,
			'url'		=> $url,
			'format'	=> $format
		);

		return $this->api_call('/bloginfo/?'.http_build_query($params));
	}

	/**
	 * blog_post_tags
	 *
	 * @param string $url
	 * @param integer $limit
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function blog_post_tags($url, $limit = '10')
	{
		$params = array
		(
			'key'	=> $this->api_key,
			'url'	=> $url,
			'limit' => $limit
		);

		return $this->api_call('/blogposttags/?'.http_build_query($params));
	}

	/**
	 * get_info
	 *
	 * @param string $username
	 * @param string $format
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function get_info($username, $format = 'xml')
	{
		$params = array
		(
			'key'		=> $this->api_key,
			'username'	=> $username,
			'format'	=> $format
		);

		return $this->api_call('/getinfo/?'.http_build_query($params));
	}

	/**
	 * key_info
	 *
	 * @return object SimpleXMLElement Object
	 * @access public
	 *
	 */
	public function key_info() 
	{
		return $this->api_call('/keyinfo/?key='.$this->api_key);
	}

	/**
	 * api_call
	 * 
	 * @param string $api_url
	 * @return object SimpleXMLElement Object
	 * @access protected
	 * 
	 */
	protected function api_call($api_url)
	{
		if ( ! $result = parent::api_call($api_url, false)) 
			throw new Kohana_User_Exception('Technorati Driver', Kohana::lang('service.invalid_response', $this->last_api_call(), $this->last_status_code()));
	
		$xml = simplexml_load_string($result);
	
		if (false === $xml) 
			throw new Kohana_User_Exception('Technorati Driver', Kohana::lang('service.xml_parse_error'));

		return $xml;
	}

} //End Technorati Driver